package com.redteam.data.dao;

import com.redteam.data.beans.CharterCustomer;
import com.redteam.data.beans.CustomerPilot;
import com.redteam.data.beans.HouseAccount;
import com.redteam.data.beans.Student;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * DAO for <code>HouseAccounts</code>
 * @author Jezreel Wilson
 * @author Steven Lawton
 * @version 1.0
 */
public class HouseAccountDAO extends AbstractDAO implements DAO {

    private static HouseAccountDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new HouseAccountDAO();
    }

    private HouseAccountDAO() {
        getDataSource();
        tableName = "HIGHFLIERS.HouseAccounts";
    }

    /**
     * Static method for receiving <code>HouseAccountDAO</code>
     * @return
     * HouseAccountDAO
     */
    public static HouseAccountDAO getInstance() {
        return dao;
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(HouseAccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildUpdateQuery(Object obj) {
        HouseAccount house = (HouseAccount) obj;
        StringBuilder sb = new StringBuilder("UPDATE ").append(this.getTableName());
        sb.append(" SET BALANCE = ").append(house.getBalance());
        sb.append(", DUE_DATE = '").append(house.getDueDate());
        sb.append("' WHERE ID = ").append(house.getAccountNumber());
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public HouseAccount getObjectOut(ResultSet rs) {
        HouseAccount house = new HouseAccount();
        try {
            PersonsDAO pdao = PersonsDAO.getInstance();
            long personId = rs.getLong("PERSON_ID");
            if (pdao.personDescriminator(personId).equals("stud")) {
                DAO dao2 = StudentDAO.getInstance();
                house.setPerson((Student) dao2.getSingle(personId));
            } else if (pdao.personDescriminator(personId).equals("cust")) {
                DAO dao2 = CharterCustomerDAO.getInstance();
                house.setPerson((CharterCustomer) dao2.getSingle(personId));
            } else if (pdao.personDescriminator(personId).equals("pilot")) {
                DAO dao2 = CustomerPilotDAO.getInstance();
                house.setPerson((CustomerPilot) dao2.getSingle(personId));
            }
            house.setAccountNumber(rs.getLong("ID"));
            house.setBalance(rs.getDouble("BALANCE"));
            house.setDueDate(rs.getDate("DUE_DATE"));
        } catch (SQLException ex) {
            Logger.getLogger(HouseAccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return house;
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildInsertQuery(Object obj) {
        HouseAccount house = (HouseAccount) obj;
        StringBuilder sb = new StringBuilder("INSERT INTO ").append(this.getTableName());
        sb.append(" (PERSON_ID,BALANCE,DUE_DATE");
        sb.append(") VALUES (").append(house.getPerson().getId()).append(",").append(house.getBalance());
        sb.append(",'").append(house.getDueDate()).append("')");
        return sb.toString();
    }
    /**
     * Gets <code>Customer</code> account
     * @param customerID
     * Id of <code>Customer</code>
     * @return
     * <code>Customer</code> <code>HouseAccount</code>
     * @throws SQLException
     */
    public HouseAccount getCustomerAccount(long customerID) throws SQLException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        HouseAccount account = null;
        try {
            conn = this.getDS().getConnection();
            st = conn.createStatement();
            StringBuilder sb = new StringBuilder("SELECT * FROM ").append(this.getTableName()).append(" WHERE PERSON_ID = ");
            sb.append(customerID);
            rs = st.executeQuery(sb.toString());
            while (rs.next()) {
                account = this.getObjectOut(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(HouseAccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConnection(rs, st, conn);
            return account;
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public long getObjectId(Object o) {
        HouseAccount house = (HouseAccount) o;
        return house.getAccountNumber();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public DataSource getDS() {
        return ds;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String getTableName() {
        return tableName;

    }
}
